import React, { PureComponent } from "react";
import { connect } from "react-redux";
import DirectOrAssignList from "components/ProjectList/Home/directOrAssignList";
import {callApi} from "app/utils";

let flag = true;
export default class extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      itemData: [],
      opt: {
        page: 1,
        size: 0
      }
    };
    this.handleScrollMore = this.handleScrollMore.bind(this);
  }
  //销毁时解绑事件
  componentWillUnmount() {
    window.removeEventListener("scroll", this.handleScrollMore);
    //重写组件的setState方法，直接返回空
    this.setState = (state,callback)=>{
      return;
    };  
  }
  handleScrollMore() {
    if ($(window).scrollTop() != 0) {
      if (
        $(window).scrollTop() + window.innerHeight >=document.documentElement.scrollHeight && flag == true
      ) {
        flag = false;
        let page = this.state.opt.page;
        page++;
        this.setState({
          opt: {
            page: page
          }
        });
        let tempList = this.state.itemData;
       
        callApi('/loan/credit/list',{page:this.state.opt.page, size:10},"POST").then((data)=>{
          if (data.msg == "查询结果为空！") {
                flag = true
                return;
              }
              this.setState({
                itemData: tempList.concat(data.data.creditList)
              });
              flag = true
        }).catch(()=>{
            flag=true
          });
      }
    }
  }

  componentDidMount() {
    window.addEventListener("scroll", this.handleScrollMore);
    callApi('/loan/credit/list',{page:this.state.opt.page, size:10},"POST").then((data)=>{
      this.setState({
        itemData: data.data.creditList
      });
    })
  }

  render() {
    const { itemData } = this.state;
    let {directOrAssign} = this.props;
    return (
      <div>
        <DirectOrAssignList itemData={itemData} directOrAssign={directOrAssign} />
      </div>
    );
  }
}
